using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
namespace HotUpdate.Editor {
    [CustomPropertyDrawer(typeof(AnimatorStateNameAttribute))]
    public class AnimatorStateNameAttributeDrawer : PropertyDrawer
    {
        List<string> m_AnimationNames = new List<string>();
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            m_AnimationNames.Clear();
            var mono = (property.serializedObject.targetObject as MonoBehaviour);
            Animator animator = null;           
            if (mono!=null)
            {
                var attr = attribute as AnimatorStateNameAttribute;
                if(string.IsNullOrEmpty(attr.AnimatorFieldName))
                {
                    animator = mono.GetComponentInChildren<Animator>();
                }else
                {
                    var animatorProperty = property.serializedObject.FindProperty(attr.AnimatorFieldName);
                    if(animatorProperty!=null && animatorProperty.objectReferenceValue!=null)
                    {
                        animator = animatorProperty.objectReferenceValue as Animator;
                    }
                }
            }
            if(animator == null)
            {
                property.stringValue = EditorGUI.TextField(position,property.displayName,property.stringValue);
            }else
            {
                SerializedObject serializedObject = new SerializedObject(animator);
                var controller = serializedObject.FindProperty("m_Controller");
                AnimatorController animatorController = controller.objectReferenceValue as AnimatorController;
                if (animatorController != null)
                {
                    foreach (var layer in animatorController.layers)
                    {
                        var states = layer.stateMachine.states;
                        foreach (var state in states)
                        {
                            if (!m_AnimationNames.Contains(state.state.name))
                            {
                                m_AnimationNames.Add(state.state.name);
                            }
                        }
                    }
                    int nameIndex = -1;
                    for (int i = 0; i < m_AnimationNames.Count; i++)
                    {
                        if (m_AnimationNames[i] == property.stringValue)
                        {
                            nameIndex = i;
                        }
                    }
                    int selectedIndex = EditorGUI.Popup(position, property.displayName, nameIndex, m_AnimationNames.ToArray());
                    if (selectedIndex != nameIndex)
                    {
                        property.stringValue = m_AnimationNames[selectedIndex];
                    }
                }
                else
                {
                    property.stringValue = EditorGUILayout.TextField(property.displayName, property.stringValue);
                }
                
            }            
        }               
    }

}

